package com.itheima.controller;

import com.itheima.pojo.Clazzs;
import com.itheima.pojo.PageBody;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/clazzs")
public class ClazzsController {

    @Autowired
    private ClazzsService clazzsService;
    @GetMapping
    public Result findClazzs(@RequestParam(value = "name",required = false) String name,
                             @RequestParam(value = "begin",required = false)String begin,
                             @RequestParam(value = "end",required = false)String end,
                             @RequestParam(value = "page",defaultValue = "1")Integer page,
                             @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
       PageBody pageBody  =  clazzsService.findClazzsByPage(name,begin,end,page,pageSize);
        return Result.success(pageBody);
    }
    @GetMapping("/list")
    public Result list(){
        List<Clazzs> all = clazzsService.findAll();
        return Result.success(all);
    }
    @PostMapping
    public Result saveClazzs(@RequestBody Clazzs clazzs){
        clazzsService.saveClazzs(clazzs);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result saveClazzs(@PathVariable("id")Integer id){
       Clazzs clazzs =  clazzsService.findClazzsById(id);
        return Result.success(clazzs);
    }
    @PutMapping
    public Result updateClazzs(@RequestBody Clazzs clazzs){
        clazzsService.updateClazzsById(clazzs);
        return Result.success(clazzs);
    }
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id")Integer id){
        clazzsService.deleteById(id);
        return Result.success();
    }
}
